﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using LiuYan.Base;

namespace LiuYan.Interface
{
    public class WxMenu
    {
        /// <summary>
        /// 获取微信菜单
        /// </summary>
        /// <param name="access_token"></param>
        /// <returns></returns>
        public string GetMenu(string access_token)
        {
           var temp = "";
           var url = string.Format(WxEntity.getMenuUrl, access_token);
            HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
            using (Stream resStream1 = response1.GetResponseStream())
            {
                StreamReader reader = new StreamReader(resStream1, Encoding.UTF8);
                temp = reader.ReadToEnd();
                resStream1.Close();
            }
            return temp;
        }

        /// <summary>
        /// 创建微信菜单
        /// </summary>
        /// <param name="menuData"></param>
        /// <param name="access_token"></param>
        /// <returns></returns>
        public string CreatMenu(string menuData,string access_token)
        {
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = Encoding.UTF8;
            byte[] data = encoding.GetBytes(menuData);
            // 准备请求...
            try
            {
                var url = String.Format(WxEntity.creatMenuUrl, access_token);
                // 设置参数
                request = WebRequest.Create(url) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                //返回结果网页（html）代码
                string content = sr.ReadToEnd();
                string err = string.Empty;
                 return content;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                //return string.Empty;
            }
            return "error";
        }

        /// <summary>
        /// 删除微信菜单
        /// </summary>
        /// <param name="access_token"></param>
        /// <returns></returns>
        public string DelMenu(string access_token)
        {
            string url = String.Format(WxEntity.delMenuUrl, access_token);
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, encoding);
            string content = sr.ReadToEnd();
            return content;
        }
    }
}
